package com.unswift.window.extend;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;

import com.unswift.utils.ObjectUtils;

@SuppressWarnings("serial")
public class Button extends JButton implements MouseListener{

	private Color initBgColor;
	
	public Button() {
		super();
		this.addMouseListener(this);
	}

	public Button(String text) {
		super(text);
		this.addMouseListener(this);
	}
	
	public Button(ImageIcon icon) {
		super(icon);
		this.addMouseListener(this);
	}
	
	public Button(String text, ImageIcon icon) {
		super(text, icon);
		this.addMouseListener(this);
	}
	
	@Override
	public void setBackground(Color bg) {
		super.setBackground(bg);
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D) g.create();
		int width = getWidth();
		int height = getHeight();
		g2d.setColor(getBackground());
		g2d.fillRect(0, 0, width, height);
		g2d.dispose();
		super.paintComponent(g);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}
	
	@Override
	public void mouseExited(MouseEvent e) {
		if(ObjectUtils.isEmpty(initBgColor)){
			initBgColor=Color.WHITE;
		}
		JButton button=(JButton)e.getComponent();
		Insets padding = button.getBorder().getBorderInsets(button.getParent());
		button.setBorder(BorderFactory.createMatteBorder(padding.top, padding.left, padding.bottom, padding.right, initBgColor));
		button.setBackground(initBgColor);
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		if(ObjectUtils.isEmpty(initBgColor)){
			initBgColor=Color.WHITE;
		}
		JButton button=(JButton)e.getComponent();
		Insets padding = button.getBorder().getBorderInsets(button.getParent());
		button.setBorder(BorderFactory.createMatteBorder(padding.top, padding.left, padding.bottom, padding.right, new Color(204, 232, 255)));
		button.setBackground(new Color(229, 243, 255));
	}

	public void setInitBgColor(Color initBgColor) {
		this.initBgColor = initBgColor;
		this.setBackground(initBgColor);
		Insets padding = this.getBorder().getBorderInsets(this.getParent());
		this.setBorder(BorderFactory.createMatteBorder(padding.top, padding.left, padding.bottom, padding.right, initBgColor));
	}
}
